<?php
namespace app\article\controller;
use think\Controller;
use app\article\model\Index as Article;
use think\Db;

class Index extends controller
{
	public function index()
	{
		global $_W;
		if (empty($_W['user']['uid'])) {
			exit('请登录！');
		}
		$this->assign('_W', $_W);

		return $this->fetch();
	}

	public function system()
	{
		$webinfo = array();
		$webinfo['os'] = PHP_OS;
		$webinfo['version'] = PHP_VERSION;
		$webinfo['sapi'] =php_uname();
		$webinfo['IP'] = GetHostByName($_SERVER['SERVER_NAME']);
		$webinfo['hostip'] = $_SERVER['REMOTE_ADDR'];
		$webinfo['domain'] = $_SERVER['HTTP_HOST'];
		$mysql_version = Db::query('SELECT VERSION() as version;');
		$webinfo['mysql'] = empty($mysql_version[0]['version']) ? '未知' : $mysql_version[0]['version'];
		$this->assign('webinfo', $webinfo);

		return $this->fetch();
	}

	public function article()
	{
		if($this->request->isAjax()){
			$param = $this->request->param();
			$pageindex = max($param['page'], 1) - 1;
			$pagesize = empty($param['limit']) ? 20 : $param['limit'];
			$where = array();
			if (!empty($param['keyword'])) {
				$where['title'] = $param['keyword'];
			}
			$article_info = Article::where($where)->limit($pageindex, $pagesize)->order('id', 'desc')->select();
			$article_count = Article::where($where)->count();

			$data['code'] = 0;
			$data['data'] = $article_info;
			$data['count'] = $article_count;
			$data['msg'] = 'success';
			return $data;
		}else{
			return $this->fetch();
		}
	}

	public function article_add()
	{
		$param = $this->request->param();
		if ($this->request->isAjax()) {
			if (empty($param['title']) || empty($param['description']) || empty($param['article'])) {
				return array('code' => -1, 'data' => array(), 'msg' => '文章数据不完整！');
			}

			$article = new Article();
			$article->title = $param['title'];
			$article->aouth = empty($param['aouth']) ? '微擎团队' : $param['aouth'];
			$article->description = $param['description'];
			$article->article = $param['article'];
			$article->creattime = time();
			$article->save();

			if (empty($article->id)) {
				return array('code' => -1, 'data' => array(), 'msg' => '文章添加失败！');
			}
			return array('code' => 0, 'data' => array(), 'msg' => '文章添加成功！');
		}
		$article_info = array();
		if (!empty($param['id'])) {
			$article_info = Article::get(intval($param['id']));
		}
		$this->assign('article_info', $article_info);

		return $this->fetch();
	}

	public function article_edit()
	{
		$param = $this->request->param();
		if (empty($param['id'])) {
			return array('code' => -1, 'data' => array(), 'msg' => '文章不存在！');
		}
		if ($this->request->isAjax()) {
			if (empty($param['title']) || empty($param['description']) || empty($param['article'])) {
				return array('code' => -1, 'data' => array(), 'msg' => '文章数据不完整！');
			}

			$article = array(
				'id' => $param['id'],
				'title' => $param['title'],
				'aouth' => empty($param['aouth']) ? '微擎团队' : $param['aouth'],
				'description' => $param['description'],
				'article' => $param['article'],
			);
			Article::update($article);

			return array('code' => 0, 'data' => array(), 'msg' => '文章编辑成功！');
		}

		$article_info = Article::get(intval($param['id']));
		$this->assign('article_info', $article_info);

		return $this->fetch();
	}

	public function article_del()
	{
		$param = $this->request->param();
		if (empty($param['id'])) {
			return array('code' => -1, 'data' => array(), 'msg' => '文章不存在！');
		}
		Article::destroy(intval($param['id']));

		return array('code' => 0, 'data' => array(), 'msg' => '文章删除成功！');
	}
}
